<?php
/**
 * 微信公众号消息回复类
 * @author lyh/508291869@qq.com
 */
namespace wechat;
use wechat\wechat_adv;
class wechat_msg
{
	public $appid = "";
    public $appsecret = "";
	public $adv = "";
    public function __construct($appid = NULL, $appsecret = NULL)
    {
        if($appid){
            $this->appid = $appid;
        }
        if($appsecret){
            $this->appsecret = $appsecret;
        }
		$this->adv = new wechat_adv($this->appid, $this->appsecret);
    }
	/*
     * 回复文本消息
     */
    public function text($object, $content)
    {
        $textTpl = "<xml>
<ToUserName><![CDATA[%s]]></ToUserName>
<FromUserName><![CDATA[%s]]></FromUserName>
<CreateTime>%s</CreateTime>
<MsgType><![CDATA[text]]></MsgType>
<Content><![CDATA[%s]]></Content>
</xml>";
        $result = sprintf($textTpl, $object->FromUserName, $object->ToUserName, time(), $content);
        echo $result;
    }
	/*
     * 回复图片消息
     */
    public function image($object,$filepath)
    {
    	$img = $this->adv->upload_media('image', $filepath);
		$media_id = '';
		if(empty($img['errcode']))
		{
			$media_id = $img['media_id'];
		}
        $itemTpl = "<Image>
    <MediaId><![CDATA[%s]]></MediaId>
</Image>";
        $item_str = sprintf($itemTpl, $media_id);
        $textTpl = "<xml>
<ToUserName><![CDATA[%s]]></ToUserName>
<FromUserName><![CDATA[%s]]></FromUserName>
<CreateTime>%s</CreateTime>
<MsgType><![CDATA[image]]></MsgType>
$item_str
</xml>";
        $result = sprintf($textTpl, $object->FromUserName, $object->ToUserName, time());
        echo $result;
    }
	/*
     * 回复语音消息
     */
    public function voice($object,$filepath)
    {
    	$voice = $this->adv->upload_media('voice', $filepath);
		$media_id = '';
		if(empty($voice['errcode']))
		{
			$media_id = $voice['media_id'];
		}
        $itemTpl = "<Voice>
    <MediaId><![CDATA[%s]]></MediaId>
</Voice>";
        $item_str = sprintf($itemTpl, $media_id);
        $textTpl = "<xml>
<ToUserName><![CDATA[%s]]></ToUserName>
<FromUserName><![CDATA[%s]]></FromUserName>
<CreateTime>%s</CreateTime>
<MsgType><![CDATA[voice]]></MsgType>
$item_str
</xml>";
        $result = sprintf($textTpl, $object->FromUserName, $object->ToUserName, time());
        echo $result;
    }
    
    /*
     * 回复视频消息
     */
    public function video($object,$filepath, $title = '',$description = '')
    {
    	$video = $this->adv->upload_media('video', $filepath);
		$media_id = '';
		if(empty($video['errcode']))
		{
			$media_id = $video['media_id'];
		}
        $itemTpl = "<Video>
    <MediaId><![CDATA[%s]]></MediaId>
    <ThumbMediaId><![CDATA[%s]]></ThumbMediaId>
    <Title><![CDATA[%s]]></Title>
    <Description><![CDATA[%s]]></Description>
</Video>";

        $item_str = sprintf($itemTpl, $media_id, $object->ThumbMediaId, $title, $description);//标题描述
        $textTpl = "<xml>
<ToUserName><![CDATA[%s]]></ToUserName>
<FromUserName><![CDATA[%s]]></FromUserName>
<CreateTime>%s</CreateTime>
<MsgType><![CDATA[video]]></MsgType>
$item_str
</xml>";
        $result = sprintf($textTpl, $object->FromUserName, $object->ToUserName, time());
        echo $result;
    }
    
	/*
     * 回复音乐消息
     */
    public function music($object, $musicArray)
    {
        $itemTpl = "<Music>
    <Title><![CDATA[%s]]></Title>
    <Description><![CDATA[%s]]></Description>
    <MusicUrl><![CDATA[%s]]></MusicUrl>
    <HQMusicUrl><![CDATA[%s]]></HQMusicUrl>
</Music>";
		$item_str = sprintf($itemTpl, $musicArray['Title'], $musicArray['Description'], $musicArray['MusicUrl'], $musicArray['HQMusicUrl']);
        $textTpl = "<xml>
<ToUserName><![CDATA[%s]]></ToUserName>
<FromUserName><![CDATA[%s]]></FromUserName>
<CreateTime>%s</CreateTime>
<MsgType><![CDATA[music]]></MsgType>
$item_str
</xml>";
        $result = sprintf($textTpl, $object->FromUserName, $object->ToUserName, time());
        echo $result;
    }
    /*
     * 回复图文消息
     */
    public function news($object, $news_arr)
    {
    	//回复多图文消息
        $content = array();
		if(!is_array($news_arr))
		{
			return '';	
		}
        $itemTpl = "    <item>
        <Title><![CDATA[%s]]></Title>
        <Description><![CDATA[%s]]></Description>
        <PicUrl><![CDATA[%s]]></PicUrl>
        <Url><![CDATA[%s]]></Url>
    </item>
";
        $item_str = "";
        foreach ($news_arr as $item)
            $item_str .= sprintf($itemTpl, $item['Title'], $item['Description'], $item['PicUrl'], $item['Url']);
        $newsTpl = "<xml>
<ToUserName><![CDATA[%s]]></ToUserName>
<FromUserName><![CDATA[%s]]></FromUserName>
<CreateTime>%s</CreateTime>
<MsgType><![CDATA[news]]></MsgType>
<Content><![CDATA[]]></Content>
<ArticleCount>%s</ArticleCount>
<Articles>
$item_str</Articles>
</xml>";
        $result = sprintf($newsTpl, $object->FromUserName, $object->ToUserName, time(), count($news_arr));
        echo $result;
    }
}
/*使用方法
 * $msg = new wechat_msg($this->appid, $this->appsecret);//实例化
 * $msg->text($postObj,'文本消息');//回复文本消息
 * $msg->image($postObj,'D:/image.jpg');//回复图片消息
 * $msg->voice($postObj,'D:/voice.mp3');//回复语音消息
 * $msg->video($postObj,'D:/video.mp4','视频标题','视频描述');//回复视频消息
 * $musicArray = array(
 * 	   "Title"=>"夜空中最亮的星", 
 *     "Description"=>"邓紫棋", 
 *     "MusicUrl"=>"http://www.webset.top/music.mp3",
 *     "HQMusicUrl"=>"http://www.webset.top/music.mp3",
 * );
 * $msg->music($postObj,$musicArray);//回复音乐消息
 * $news_arr = array(
 * 	   array(
 * 			"Title"=>'标题',//
 * 			"Description"=>'描述',//
 * 			"PicUrl"=>'http://www.webset.top/image.jpg',//封面图
 * 			"Url" =>'http://www.webset.top/image.jpg'//跳转链接
 * 	   ),
 *     array(
 * 			"Title"=>'标题',
 * 			"Description"=>'描述',
 * 			"PicUrl"=>'http://www.webset.top/image.jpg',
 * 			"Url" =>'http://www.webset.top/image.jpg'
 *     )
 * );
 * $msg->news($postObj, $news_arr);//回复图文消息,图文消息个数；当用户发送文本、图片、视频、图文、地理位置这五种消息时，开发者只能回复1条图文消息；其余场景最多可回复8条图文消息
 * 
 */